package com.heima.article.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heima.article.constants.WmNewsMsgConstants;
import com.heima.article.service.IApArticleConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArticleIsDownListener {

    @Autowired
    private IApArticleConfigService apArticleConfigService;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @KafkaListener(topics = WmNewsMsgConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message) throws JsonProcessingException {
        if (StringUtils.isNotBlank(message)) {
            Map<String, Object> map = objectMapper.readValue(message,
                    new TypeReference<Map<String, Object>>() {
            });

            apArticleConfigService.updateByMap(map);
        }
    }
}
